package com.robotserver.orders.jobs.xxljob;

import com.robotserver.orders.common.DateUtil;
import com.robotserver.orders.param.PddOrderSyncParam;
import com.robotserver.orders.service.ListenTokenRunService;
import com.robotserver.orders.service.PddOrderService;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class PddOrderSyncXxlJob {
    private static Logger logger = LoggerFactory.getLogger(PddOrderSyncXxlJob.class);

    @Autowired
    private ListenTokenRunService listenTokenRunService;
    @Autowired
    private PddOrderService pddOrderService;

    /**
     * 调用工具商拉取订单
     * 每30秒钟执行一次拉取订单（时间范围是3分钟）
     */
    @XxlJob("pushPddOrdersByAppKeyHandler")
    public void pushPddOrdersByAppKeyHandler(){
        //默认持续时间为1分钟
        Long duration = 60L;
        String durationStr = "";
        try {
            //接受参数-拉取当前日期往前的几秒订单
            durationStr = XxlJobHelper.getJobParam();
            if(StringUtils.isNotBlank(durationStr)){
                //从XXL-Job接受参数
                duration = Long.parseLong(durationStr);
            }
        }catch (Exception e){
            logger.error("xxl-job para set is error {}:"+durationStr);
            //默认设置一分钟
            duration = 60L;
        }
        Integer tmptime = DateUtil.getNowTime();
        Long end_time = Long.valueOf(tmptime);                //截至时间为当前时间
        Long begin_time = tmptime - duration;                 //起始时间为前1分钟
        PddOrderSyncParam pddOrderSyncParam = new PddOrderSyncParam();
        pddOrderSyncParam.setBegin_time(begin_time);
        pddOrderSyncParam.setEnd_time(end_time);
        pddOrderService.syncOrder(pddOrderSyncParam);
    }

}
